

var app = getApp();
Page({
  data: {
    showmodal:false,
    successssubmit:false,
    threemiao:false,
    nowshowcontent:undefined,
    message_arr:[
      {url:1},
      {url:2},
      {url:3},
    ]
  },
  onLoad: function (e) {
    var vm = this;
    vm.setData({
      id:e.id,
    });
    var userid = getApp().globalData.openid;
      var data = 
        {
          
        }
      wx.request({
        url: getApp().globalData.baseUrl + '/wago/safety/selectByTime',
        method: 'post',
        data,
        success: function (res) {
          vm.setData({
            list: res.data.data,
            recommedarr:res.data.data.slice(0,10)
          })
          var nowlist = vm.data.list
          var namearr= nowlist.map((item,index) =>{
              return item.id
          })
          var nowindex = namearr.indexOf(e.id);
          var max = namearr.length-1;
          var nextid = nowindex != max ? nowindex+1 : 0;
          var previd = nowindex  == 0 ? max : nowindex-1;
          vm.setData({
            id:e.id,
            list:nowlist,
            previd:nowlist[previd].id,
            nextid:nowlist[nextid].id,
          });
          console.log(vm.data,2580)
      
       
           vm.getArticleInfo(vm.data.id)


          
        }
      })
  

    // var nowlist = this.data.list
    // var namearr= nowlist.map((item,index) =>{
    //     return item.electricitySafety.id
    // })
    // var nowindex = namearr.indexOf(e.id);
    // var max = namearr.length-1;
    // var nextid = nowindex != max ? nowindex+1 : 0;
    // var previd = nowindex  == 0 ? max : nowindex-1;
    // this.setData({
    //   id:e.id,
    //   list:nowlist,
    //   previd:nowlist[previd].electricitySafety.id,
    //   nextid:nowlist[nextid].electricitySafety.id,
    // });

 
    //  this.getArticleInfo(this.data.id)
  },
  onShow:function(){


  
  },
  login:function(){
   
  },
  goNextArticle:function(){
    var  nowlist = this.data.list;
    var namearr= nowlist.map((item,index) =>{
      return item.id
  })
  var nowindex = namearr.indexOf(this.data.id);
  var max = namearr.length-1;
  var nextid = nowindex != max ? nowindex+1 : 0;
  var previd = nowindex  == 0 ? max : nowindex-1;
    this.setData({
      previd:nowlist[previd].id,
      nextid:nowlist[nextid].id,
    });
    this.getArticleInfo(this.data.nextid)
  },
  goPrevArticle:function(){
    var  nowlist = this.data.list;
    var namearr= nowlist.map((item,index) =>{
      return item.id
  })
  var nowindex = namearr.indexOf(this.data.id);
  var max = namearr.length-1;
  var nextid = nowindex != max ? nowindex+1 : 0;
  var previd = nowindex  == 0 ? max : nowindex-1;
    this.setData({
      previd:nowlist[previd].id,
      nextid:nowlist[nextid].id,
    });
    this.getArticleInfo(this.data.previd)
  },
  getArticleInfo:function(a){
    var userid = getApp().globalData.openid;
      var vm = this;
         wx.request({
      url: getApp().globalData.baseUrl + '/wago/safety/selectById',
      method: 'post',
      data:{
        id:a,
        userid
      },
      success: function (res) {
        vm.format(res.data.data.createTime)
        vm.setData({
          articleinfo:res.data.data,
          id:res.data.data.id,
          nowshowcontent: res.data.data.safetyContent.replace(/\\/g, '').replace(/\<img/gi, '<img style="max-width:100%;height:auto"').replace( /^["|'](.*)["|']$/g, "$1")


        })
        if(vm.data.nowshowcontent.match('<video')){
          var sssindex = vm.data.nowshowcontent.match('<video').index;
var eeeindex = vm.data.nowshowcontent.match('</video>').index;
var vide = vm.data.nowshowcontent.slice(sssindex,eeeindex)
var srcstart = vide.match('src=').index;
var srcend = vide.match('.mp4">').index;
vm.setData({
  videourl:vide.slice(srcstart+5,srcend)+'.mp4'
})
}

      if(getApp().globalData.openid){
        //查看积分规则
        wx.request({
          url: getApp().globalData.baseUrl + '/wago/jifen/findJifenControl',
          method: 'get',
          data:{
            id:4
          },
          success: function (res) {
               var changejifen = res.data.data.changejifen;
           var changename = res.data.data.name;
           var userid = getApp().globalData.openid;
            wx.request({
              url: getApp().globalData.baseUrl + '/wago/browse/findByUseridAndId',
              method: 'post',
              data:{
                userid:getApp().globalData.openid,
                id:vm.data.id
              },
              success: function (res) {
                if(res.data.flag){
                                             wx.request({
            url: getApp().globalData.baseUrl + '/wago/jifen/changeJifen',
            method: 'post',
            data:{
              userid,
              changename,
              changejifen
            },
            success: function (res) {
              
             
            }
          })
                }

               
              }
            })




           //改变积分
        

          }
        })
      }else{

      }

        









      }
    });
        //拉取留言
        wx.request({
          url: getApp().globalData.baseUrl + '/wago/liuyan/selectLiuYan',
          method: 'post',
          data:{
            "safetyid":a
          },
          success: function (res) {
    
            var datas = res.data.data;
            for (var i in datas) {
              datas[i].createTime = vm.format22(datas[i].createTime)
          }
                  vm.setData({
              message_arr:datas,
            })
    
          }
        })
  },
  showmsgmodal: function () {
    if(getApp().globalData.openid){
          this.setData({
      showmodal: true
    });
    }else{
      wx.showToast({
        title: '登录后才能留言',
        duration: 2000,
        icon: 'none'
      });
    }

  },
  astearea:function(e){
    this.setData({
      nowtextarea:e.detail.value
    })
 
  },
  closemesss:function(){
    this.setData({
      showmodal: false,
      successssubmit: false
    });
  },
  tolianxi:function(){
    wx.navigateTo({
  url: "/pages/contactUs/contactUs"
})
},
showsubmitmsg: function () {
  if(!this.data.nowtextarea){
    console.log(250)
    wx.showToast({
      title: '请填写留言',
      duration: 2000,
      icon: 'none'
    });
  }else{
        this.addmessage();
  var vm = this;
  this.setData({
    showmodal: false,
    successssubmit: true
  });
          //拉取留言
          wx.request({
            url: getApp().globalData.baseUrl + '/wago/liuyan/selectLiuYan',
            method: 'post',
            data:{
              "safetyid":this.data.id
            },
            success: function (res) {
      
              var datas = res.data.data;
              for (var i in datas) {
                datas[i].createTime = vm.format22(datas[i].createTime)
            }
                    vm.setData({
                message_arr:datas,
              })
      
            }
          })
  setTimeout(() => {
    vm.setData({
      successssubmit: false
    });
  }, 1000);
  }

},

  add0:function(m){return m<10?'0'+m:m },


 
format:function(shijianchuo)
{
//shijianchuo是整数，否则要parseInt转换
var time = new Date(shijianchuo);
var y = time.getFullYear();
var m = time.getMonth()+1;
var d = time.getDate();
var h = time.getHours();
var mm = time.getMinutes();
var s = time.getSeconds();
this.setData({
  createTT:y+'-'+this.add0(m)+'-'+this.add0(d)
})


},
format22: function (shijianchuo) {
  //shijianchuo是整数，否则要parseInt转换
  var time = new Date(shijianchuo);
  var y = time.getFullYear();
  var m = time.getMonth() + 1;
  var d = time.getDate();
  var h = time.getHours();
  var mm = time.getMinutes();
  var s = time.getSeconds();
  return  y + '-' + this.add0(m) + '-' + this.add0(d)+ ' ' + this.add0(h)+ ':' + this.add0(mm)



},
addmessage:function(){


  var data = {
    userid:getApp().globalData.openid,
    safetyid:this.data.id,
    message:this.data.nowtextarea
  }


  wx.request({
    url: getApp().globalData.baseUrl + '/wago/liuyan/addLiuyan',
    method: 'post',
    data,
    success: function (res) {

    

    }
  })
}

})


